/*
 * Copyright 2009 Fred Sauer
 * 
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
 * in compliance with the License. You may obtain a copy of the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software distributed under the License
 * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
 * or implied. See the License for the specific language governing permissions and limitations under
 * the License.
 */
package org.vaadin.addon.scrollpanel.client.ui.util;

import org.vaadin.addon.scrollpanel.client.ui.util.impl.DOMUtilImpl;

import com.google.gwt.core.client.GWT;
import com.google.gwt.dom.client.Element;
import com.google.gwt.user.client.ui.Widget;

/**
 * Provides DOM utility methods.
 */
public class DOMUtil {

    private static DOMUtilImpl impl;

    static {
        impl = GWT.create(DOMUtilImpl.class);
    }


    /**
     * Cancel all currently selected region(s) on the current page.
     */
    public static void cancelAllDocumentSelections() {
        impl.cancelAllDocumentSelections();
    }


    /**
     * Gets an element's CSS based 'border-left-width' in pixels or
     * <code>0</code> (zero) when the element is hidden.
     * 
     * @param elem
     *            the element to be measured
     * @return the width of the left CSS border in pixels
     */
    public static int getBorderLeft(Element elem) {
        return impl.getBorderLeft(elem);
    }

    /**
     * Gets an element's CSS based 'border-top-widget' in pixels or
     * <code>0</code> (zero) when the element is hidden.
     * 
     * @param elem
     *            the element to be measured
     * @return the width of the top CSS border in pixels
     */
    public static int getBorderTop(Element elem) {
        return impl.getBorderTop(elem);
    }

    /**
     * Gets an element's client height in pixels or <code>0</code> (zero) when
     * the element is hidden. This is equal to offset height minus the top and
     * bottom CSS borders.
     * 
     * @param elem
     *            the element to be measured
     * @return the element's client height in pixels
     */
    public static int getClientHeight(Element elem) {
        return impl.getClientHeight(elem);
    }

    public static int getClientWidth(Element elem) {
        return impl.getClientWidth(elem) - getHorizontalMargin(elem) - impl.getHorizontalBorders(elem);
    }

    public static String getEffectiveStyle(Element elem, String styleName) {
        return impl.getEffectiveStyle(elem, styleName);
    }

    public static int getHorizontalBorders(Widget widget) {
        return impl.getHorizontalBorders(widget);
    }

    public static int getHorizontalBorders(Element element) {
        return impl.getHorizontalBorders(element);
    }
    
    /**
     * Determine an element's node name via the <code>nodeName</code> property.
     * 
     * @param elem
     *            the element whose node name is to be determined
     * @return the element's node name
     */
    public static String getNodeName(Element elem) {
        return elem.getNodeName();
    }

    public static int getIntPropertyValue(Element element, String name) {
        return impl.getIntProperty(element, name);
    }
    
    public static int getVerticalBorders(Widget widget) {
        return impl.getVerticalBorders(widget);
    }
    
    public static int getVerticalBorders(Element element) {
        return impl.getVerticalBorders(element);
    }

    public static int getVerticalMargin(Element element) {
        return getIntPropertyValue(element, "paddingTop")
                + getIntPropertyValue(element, "paddingBottom");
    }

    public static int getHorizontalMargin(Element element) {
        return getIntPropertyValue(element, "paddingRight")
                + getIntPropertyValue(element, "paddingLeft");
    }


}
